#!/usr/bin/perl

use Test;
BEGIN { plan tests => 2 }

$basedir = $0;
$basedir =~ s|(.*)/[^/]*|$1|;

# Start the process to be traced.
system("mkfifo $basedir/flag");
if ( ( $pid = fork() ) == 0 ) {
    exec
"runcon -t test_ptrace_traced_t sh -c 'echo >$basedir/flag; while :; do :; done'";
    exit;
}

# Wait for it to start.
system("read -t 5 <>$basedir/flag");

# Verify that the nottracer domain cannot attach to the process.
# Should fail on the ptrace permission check.
$result = system "runcon -t test_ptrace_nottracer_t $basedir/ptrace $pid";
ok($result);    # we expect this to fail.

# Verify that the tracer domain can trace to the process.
$result = system "runcon -t test_ptrace_tracer_t $basedir/ptrace $pid";
ok( $result, 0 );

# Kill the process.
kill KILL, $pid;

# Clean up.
system "rm -f $basedir/flag";

exit;
